package com.yidu.hbatsams.action.emp;

import com.yidu.hbatsams.domain.AtsEmpLeader;
import com.yidu.hbatsams.domain.User;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.emp.AtsEmpExpertService;
import com.yidu.hbatsams.service.emp.AtsEmpLeaderService;
import com.yidu.hbatsams.service.emp.AtsEmpWordServic;
import com.yidu.hbatsams.utils.Config;
import com.yidu.hbatsams.utils.Message;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 中心领导的控制层
 */
@Controller
@RequestMapping("/UserseControllerLeader")
public class AtsEmpLeaderController {
    @Resource
    AtsEmpLeaderService atsEmpLeaderService;
    @Resource
    AtsEmpExpertService atsEmpExpertService;//调用用户表的方法
    /**
     * 分页查询全部
     * @param pageNum
     * @param pageSize
     * @param leaderName
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public Map<String, Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, String leaderName,String startTime,String endTime){
        Map<String,Object> map = new HashMap<>();
        Long count = atsEmpLeaderService.count();
        List<AtsEmpLeader> list = atsEmpLeaderService.selectAll(pageNum, pageSize,leaderName,startTime,endTime);

        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count); // 总行数
        map.put("data", list);

        return map;
    }


    /**
     * 逻辑删除
     * @param leaderId
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public Message deleteById(Integer leaderId){
        Message msg = new Message();
        AtsEmpLeader leader = atsEmpLeaderService.selectByPrimaryKey(leaderId);
        atsEmpExpertService.deleteClerk("1",String.valueOf(leader.getLeaderId()));
        atsEmpLeaderService.deleteByPrimaryKey(leaderId);

        msg.setNum(1);
        msg.setMsg("删除成功！");
        return msg;
    }

    /**
     * 新增+修改
     * @return
     */
    @ResponseBody
    @RequestMapping("addAndUpdate")
    public Message addInfo(AtsEmpLeader record, HttpSession session){
        Message msg = new Message();
        User user = new User();
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        UserVo userTwo = (UserVo) session.getAttribute("user");
        if(record.getLeaderId() != null && record.getLeaderId() > 0){
            AtsEmpLeader leader = atsEmpLeaderService.selectByPrimaryKey(record.getLeaderId());//查询修改前的信息
            User userThree = atsEmpExpertService.findClerk("1",String.valueOf(leader.getLeaderId()));
            user.setUserName(record.getLeaderName());
            user.setUserPhone(record.getPhone());
            user.setClerkId(String.valueOf(record.getLeaderId()));//设置领导ID
            user.setClerkName(record.getLeaderName());
            user.setUserValidity("有效");

            user.setUserTime(dateFormat.format(date));//设置创建时间
            user.setUserPeople(user.getUserName()); //设置创建人
            user.setUserId(userThree.getUserId());
            atsEmpExpertService.updateByPrimaryKeySelective(user);
            record.setCreater(userTwo.getUserName());//设置创建人
            atsEmpLeaderService.updateByPrimaryKey(record);

        }else if(record.getLeaderId() == null){
            record.setCreater(userTwo.getUserName());//设置创建人
            atsEmpLeaderService.insert(record);

            user.setUserId(uuid);
            user.setUserName(record.getLeaderName());
            user.setUserPwd("123456");
            user.setUserPhone(record.getPhone());
            user.setCompanyId("1");//用来区分表，1 领导，2 教练员，3 专家， 4 运动员
            user.setCompanyName("领导");
            user.setClerkId(String.valueOf(record.getLeaderId()));//设置专家ID
            user.setClerkName(record.getLeaderName());
            user.setUserValidity("有效");

            user.setUserTime(dateFormat.format(date));//设置创建时间
            user.setUserPeople(user.getUserName()); //设置创建人
            atsEmpExpertService.insert(user);

            msg.setNum(1);
        }

        msg.setMsg("操作成功！");
        return msg;
    }
    /**
     * 修改显示  根据id查询
     * @return
     */
    @RequestMapping("select")
    @ResponseBody
    public AtsEmpLeader select(Integer leaderId){
        AtsEmpLeader ats = atsEmpLeaderService.selectByPrimaryKey(leaderId);
        return ats;
    }
    /**
     * 批量删除
     * @param leaderIds
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteMany")
    public Message deleteMoreById(String leaderIds){
        String[] ids = null;
        Message msg = new Message();
        msg.setNum(1);
        //如果为批量删除
        if(leaderIds!=null && !"".equals(leaderIds)){
            ids=leaderIds.split(",");
        }
        for (int i = 0; i < ids.length; i++){
            String leaderId = ids[i];
            atsEmpExpertService.deleteClerk("1",String.valueOf(leaderId));
            atsEmpLeaderService.deleteByPrimaryKey(Integer.valueOf(leaderId));
        }
        msg.setMsg("操作成功");
        return msg;
    }
}
